package JavaCode.contest.weekly.n0_200.n142;

public class N1 {
    public double[] sampleStats(int[] count) {
        double min=256d,max=0,avg,mid;
        int more=0;
        int cnt=0;
        for (int i=0;i<count.length;i++)
        {
            cnt+=count[i];
            if(count[i]>0)
            {
                if(count[i]>count[more])
                {
                    more=i;
                }
                min=Math.min(min,i);
                max=Math.max(max,i);
            }
        }
        avg=getAvg(count,cnt);
        mid=getMid(count,cnt);
        return new double[]{min,max,avg,mid,more};
    }

    private double getMid(int[] count, int cnt) {
        int idx=cnt/2;
        if(cnt%2==0)
        {
            double res1=-1d;
            double res2=-1d;
            int len=0;
            for (int i=0;i<count.length;i++)
            {
                len+=count[i];
                if(len>=idx&&res1<0)
                {
                    res1=i;
                }
                if(len>=idx+1&&res2<0)
                {
                    res2=i;
                }
                if(res1>=0&&res2>=0)
                {
                    return (res1+res2)/2;
                }
            }
        }
        else
        {
            int len=0;
            for (int i=0;i<count.length;i++)
            {
                len+=count[i];
                if(len>idx)
                {
                    return i;
                }
            }
        }
        return -1;
    }

    private double getAvg(int[] count, double cnt) {
        double res=0d;
        for (int i=0;i<count.length;i++)
        {
            if(count[i]<=0)continue;
            res+=i*count[i]*1.0/cnt;
        }
        return res;
    }
}
/**
 *我们对 0 到 255 之间的整数进行采样，并将结果存储在数组 count 中：count[k] 意味着采样的整数为 k。
 *
 * 我们以 浮点数 数组的形式，分别返回样本的最小值、最大值、平均值、中位数和众数。其中，众数是保证唯一的。
 *
 * 我们先来回顾一下中位数的知识：
 *
 * 如果样本中的元素有序，并且元素数量为奇数时，中位数为最中间的那个元素；
 * 如果样本中的元素有序，并且元素数量为偶数时，中位数为中间的两个元素的平均值。
 *
 *
 * 示例 1：
 *
 * 输入：count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
 * 输出：[1.00000,3.00000,2.37500,2.50000,3.00000]
 * 示例 2：
 *
 * 输入：count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
 * 输出：[1.00000,4.00000,2.18182,2.00000,1.00000]
 *
 *
 * 提示：
 *
 * count.length == 256
 * 1 <= sum(count) <= 10^9
 * 计数表示的众数是唯一的
 * 答案与真实值误差在 10^-5 以内就会被视为正确答案
 */
